/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Administrator
 * Date: 2023-07-31
 * Time: 22:40
 */
class MyBlockingQueue{
    private String[] items = new String[1010];

    volatile int head = 0;
    volatile private int tail = 0;
    volatile private int size = 0;
    private Object locker  = new Object();

    //入队列
    public void put(String elem) throws InterruptedException {
        synchronized (locker){
            while (size >= items.length){
                locker.wait();
            }
            items[tail] = elem;
            tail++;
            if(tail >= items.length){
                tail = 0;
            }
            size++;
            locker.notify();
        }
    }

    //出队列
    public String take() throws InterruptedException {
        synchronized (locker) {
            while (size == 0) {
                locker.wait();
            }
            String elem = items[head];
            head++;
            if (head >= items.length) {
                head = 0;
            }
            size--;
            locker.notify();
            return elem;
        }
    }

}
public class Test1 {
    public static void main(String[] args) throws InterruptedException {
        MyBlockingQueue queue = new MyBlockingQueue();
        queue.put("a");
        queue.put("b");
        queue.put("c");

        String elem = queue.take();
        System.out.println("elem="+ elem);
        elem = queue.take();
        System.out.println("elem="+ elem);
        elem = queue.take();
        System.out.println("elem="+ elem);
    }
}
